package dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Horder;
import model.HorderPK;
import model.Hotel;
import model.Message;
import model.MessagePK;
import model.Route;
import model.Tourist;
import model.View;
import model.Vroute;
import model.VroutePK;
import util.EntityManagerUtil;

public class MessageDao {

	public static Message newMessage(String temail, String vname) throws Exception {
		Tourist tourist = TouristDao.getTouristNoCache(temail);
		View view = ViewDao.getViewNoCache(vname);
		Message message = new Message();
		MessagePK pk = new MessagePK();
		pk.setTEmail(tourist.getEmail());
		pk.setVName(view.getName());
		message.setId(pk);
		return message;
	}

	public static Message getMessage(Message message) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Message messageFind = new Message();
		messageFind = em.find(Message.class, message.getId());
		if (messageFind != null) {
			em.close();
			return messageFind;
		} else
			return null;
	}
	//自己加的
	public static Message newMessages(String temail) throws Exception {
		Tourist tourist = TouristDao.getTouristNoCache(temail);
		Message message = new Message();
		MessagePK pk = new MessagePK();
		pk.setTEmail(tourist.getEmail());
		message.setId(pk);
		return message;
	}

	public static List<Message> getMessagsByView(String vname) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Query query = em.createQuery("SELECT c FROM Message c where c.id.vName =:vname order   by c.time desc");
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		List<Message> messages = query.setParameter("vname", vname).getResultList();
		em.close();
		return messages;
	}

	public static Message getMessageNoCache(Message message) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Message c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Message messageFind;
		try {
			messageFind = (Message) query.setParameter("id", message.getId()).getSingleResult();
			em.close();
			return messageFind;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		em.close();
		return null;
	}

	public static boolean updateMessage(Message message) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Message messageFind = getMessageNoCache(message);
				if (messageFind != null) {
					Message upMessage = em.find(Message.class, message.getId());
					em.getTransaction().begin();
					upMessage.setStatus(message.getStatus());
					em.getTransaction().commit();
				em.close();
				return true;
			} else {
				System.out.println("失败");
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static boolean getMessageNoCacheForBool(Message message) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Message c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Message messageFind;
		try {
			messageFind = (Message) query.setParameter("id", message.getId()).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			em.close();
			return false;
		}

	}

	public static boolean addMessage(Message message) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			if (!getMessageNoCacheForBool(message)) {
				em.getTransaction().begin();
				em.persist(message);
				em.getTransaction().commit();
				em.close();
				return true;
			}
		} catch (Exception e) {

			return false;
		}
		return false;
	}

	public static boolean delMessage(Message message) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Message messageFind = getMessageNoCache(message);
			if (messageFind != null) {
				Message delMessage = em.find(Message.class, message.getId());
				em.getTransaction().begin();
				em.remove(delMessage);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

	public static void main(String agrs[]) throws Exception {
		Message message = new Message();
		MessagePK pk = new MessagePK();
		pk.setTEmail("t2");
		pk.setVName("fafa444");
		message.setId(pk);
		
//		MessageDao.addMessage(message);
		message.setStatus(9);
		MessageDao.updateMessage(message);
	}

}
